<?php
/**
 * Created by PhpStorm.
 * User: a
 * Date: 2016/6/2
 * Time: 17:18
 */

namespace Admin\Model;
use Think\Model;

class AuthorityModel extends Model{
    protected $trueTableName = 'bypal_comm_authority';

    /**
     * 查询菜单一级目录
     * @return mixed
     */
    public function get_authority_first_by_userid($role_id){
        $sql="select t2.authority_id,t2.authority_name,t2.authority_pid,t2.authority_url from bypal_comm_roleauthority t1
        left join bypal_comm_authority t2 on t1.authority_id=t2.authority_id where t2.authority_grade =1 and t1.role_id='$role_id' order by t2.authority_order ";
        $Model = new \Think\Model();
        $values=$Model->query($sql);
        return $values;
    }

    public function get_authority_second_by_userid($role_id){
        $sql="select t2.authority_id,t2.authority_name,t2.authority_pid,t2.authority_url from bypal_comm_roleauthority t1
        left join bypal_comm_authority t2 on t1.authority_id=t2.authority_id where t2.authority_grade =2 and t1.role_id='$role_id' order by t2.authority_order ";
        $Model = new \Think\Model();
        $values=$Model->query($sql);
        return $values;
    }

    public function get_authority_all(){
        $sql="select authority_id id,authority_pid pId,authority_name name from bypal_comm_authority";
        $Model = new \Think\Model();
        $values=$Model->query($sql);
        return $values;
    }

    public function get_authority_by_role($role_id){
        $sql="select t1.authority_id id,t1.authority_pid pId,t1.authority_name name,case when t2.id is null then 'false' else 'true' end as checked
        from bypal_comm_authority t1 left join (select authority_id,id from bypal_comm_roleauthority where role_id=$role_id) t2
        on t1.authority_id=t2.authority_id ";
        $Model = new \Think\Model();
        $values=$Model->query($sql);
        return $values;
    }

    public function get_button_authority($pathname,$role_id){
        $sql="select t2.button_function,t2.button_type from bypal_comm_roleauthority t1 left join bypal_comm_authority t2 on t1.authority_id=t2.authority_id
        where t2.authority_grade=3 and t1.role_id='$role_id' and t2.authority_url='$pathname' order by t2.authority_order ";
        $Model = new \Think\Model();
        $values=$Model->query($sql);
        return $values;
    }


}